package me.zhengjie.server.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import me.zhengjie.controller.WcsToSendWmsController;
import me.zhengjie.dao.*;
import me.zhengjie.enums.ConstantStatus;
import me.zhengjie.mapper.MaterialMapper;
import me.zhengjie.mapper.OutBoundTaskMapper;
import me.zhengjie.mapper.OutboundOrderMapper;
import me.zhengjie.mapper.SortingMapper;
import me.zhengjie.server.*;
import me.zhengjie.utils.*;
import me.zhengjie.vo.OutBoundTaskToWcsVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author xinglin.lu
* @description 针对表【iws_out_bound_task(出库任务)】的数据库操作Service实现
* @createDate 2025-06-10 08:54:56
*/
@Service
@AllArgsConstructor
public class OutBoundTaskServiceImpl extends ServiceImpl<OutBoundTaskMapper, OutBoundTaskDao>
    implements OutBoundTaskService {

    private final MaterialMapper materialMapper;

    private final MaterialService materialService;

    private  final StoreOutsideService storeOutsideService;

    private final ContainerService containerService;

    private final SortingService sortingService;

    private final SortingMapper sortingMapper;

    private final LackService lackService;

    private final EndContainerService endContainerService;

    private final OutBoundTaskMapper outBoundTaskMapper;

    private final StoreLocalService storageServer;

    private final DispositionService dispositionService;

    private final StoreLocalService storeLocalService;

    private final OutboundOrderMapper outboundOrderService;



    @Override
    public PageResult<OutBoundTaskDao> queryAll(OutBoundTaskDao criteria, Page<OutBoundTaskDao> page) {
        LambdaQueryWrapper<OutBoundTaskDao> outBoundTaskList = new LambdaQueryWrapper<OutBoundTaskDao>().eq(OutBoundTaskDao::getCode, criteria.getCode())
                .eq(criteria.getOutWarehouse() != null, OutBoundTaskDao::getOutWarehouse, criteria.getOutWarehouse())
                .eq(criteria.getOutboundType() != null, OutBoundTaskDao::getOutboundType, criteria.getOutboundType())
                .eq(criteria.getOutboundPolicy() != null, OutBoundTaskDao::getOutboundPolicy, criteria.getOutboundPolicy())
                .eq(criteria.getStatus() != null, OutBoundTaskDao::getStatus, criteria.getStatus())
                .eq(criteria.getRequireTime() != null, OutBoundTaskDao::getRequireTime, criteria.getRequireTime())
                .ge(criteria.getOutWarehouse() != null, OutBoundTaskDao::getCreateTime, criteria.getCreateTime())
                .le(criteria.getOutWarehouse() != null, OutBoundTaskDao::getCreateTime, criteria.getCreateTime())
                .orderByDesc(OutBoundTaskDao::getOutboundPolicy);
        IPage<OutBoundTaskDao> pageResult = baseMapper.selectPage(page, outBoundTaskList);
        return PageUtil.toPage(pageResult);
    }

    @Override
    public List<OutBoundTaskDao> queryAll(OutBoundTaskDao criteria) {
        return baseMapper.findAll(criteria);
    }


    @Override
    public void download(List<OutBoundTaskDao> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OutBoundTaskDao outBoundTaskDao : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("任务单号", outBoundTaskDao.getCode());
            map.put("出库任务类型", outBoundTaskDao.getOutboundType());
            map.put("入库任务状态", outBoundTaskDao.getStatus());
            map.put("优先级", outBoundTaskDao.getPriority());
            map.put("创建时间", outBoundTaskDao.getCreateTime());
            map.put("完成时间", outBoundTaskDao.getFinishTime());
            map.put("任务起点", outBoundTaskDao.getTaskStart());
            map.put("任务终点", outBoundTaskDao.getTaskEnd());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void add(OutBoundTaskDao resources) {
        baseMapper.insert(resources);
    }
    //分拣
//    @Override
//    public void startOutBoundTask(OutBoundTaskDao outBoundTaskDao) throws InterruptedException {
//        //判断是否为紧急任务
//        if (outBoundTaskDao.getPriority() == 1) {
//            List<OutBoundTaskDao> outBoundTasks = baseMapper.selectList(new LambdaQueryWrapper<OutBoundTaskDao>().eq(OutBoundTaskDao::getStatus, 1));
//            if (!outBoundTasks.isEmpty()) {
//                throw new RuntimeException("当前有其他任务正在执行该优先出库任务待插队执行");
//            }else {
//                outBoundTaskDao.setMaterHouse("分拣区");
//            }
//        }
//        //预约任务
//        if (outBoundTaskDao.getPriority()==2){
//            if (outBoundTaskDao.getOutWarehouse().equals("2F")){
//                if (outBoundTaskDao.getRequireTime().before(new Date())){
//                    outBoundTaskDao.setMaterHouse("理货区");
//                }
//                outBoundTaskDao.setMaterHouse("分拣区");
//            }
//        }
//        Material material = materialMapper.selectOne(new LambdaQueryWrapper<Material>().eq(Material::getMaterialCode, outBoundTaskDao.getMaterialCode()));
//        if (material == null) {
//            throw new RuntimeException("该物料不存在");
//        }
//        //分配储位
//        StoreOutsideDao outside = storeOutsideService.getOne(new LambdaQueryWrapper<StoreOutsideDao>().eq(StoreOutsideDao::getStoreType, "分拣区"));
//        if (outside!=null){
//            outBoundTaskDao.setOutsideId(outside.getId());
//        }else {
//            System.out.println("等待储位10秒");
//            Thread.sleep(10000);
//        }
//        List<Container> containerList = containerService.list(new LambdaQueryWrapper<Container>().eq(Container::getId, material.getContainerCode()));
//        if (containerList.isEmpty()) {
//            throw new RuntimeException("该物料库存不足");
//        }
//        for (Container container : containerList) {
//            StoreLocalDao storeLocal = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getId, container.getStoreId()));
//            WareHouse wareHouse = wareHouseService.getOne(new LambdaQueryWrapper<WareHouse>().eq(WareHouse::getFloor, storeLocal.getWareFloor()));
//            if(wareHouse.getFrame().equals("2F")){
//                if ((outBoundTaskDao.getMaterilaNweight()-container.getWeight())<0){
//                    System.out.println("正向拣选");
//                }else {
//                    System.out.println("整箱出库");
//                }
//                System.out.println("整箱与正向分拣");
//            }else {
//                if ((outBoundTaskDao.getMaterilaNweight()-container.getWeight())<container.getWeight()/2){
//                    System.out.println("反向拣选");
//                }else if((outBoundTaskDao.getMaterilaNweight()-container.getWeight())>container.getWeight()/2){
//                    System.out.println("正向拣选");
//                }else {
//                    System.out.println("整板出库");
//                }
//            }
//
//        }
//
//    }
     //出库单任务开始
    @Transactional(rollbackFor = Exception.class)
    public void startOutBoundTask(OutBoundTaskDao outBoundTaskDao) throws InterruptedException {
        try {
            // 验证任务合法性
            validateTask(outBoundTaskDao);

            // 设置任务区域
            setTaskArea(outBoundTaskDao);
            // 分配储位
            assignStorageLocation(outBoundTaskDao);
            // 选择分拣策略
            selectSortingStrategy(outBoundTaskDao);

            // 更新任务状态
            outBoundTaskDao.setStatus(2); // 2 表示执行中
            outBoundTaskDao.setCreateTime(new Date());
            baseMapper.updateById(outBoundTaskDao);

        }  catch (Exception e) {
            throw new RuntimeException("任务执行失败: " + e.getMessage(), e);
        }
    }

    @Override
    public void sendToWcs(OutBoundTaskToWcsVo outBoundTaskToWcsVo) throws IOException {
        String url = "http://localhost:8005//delivery/task";
        // 发送POST请求
        String response = HttpUtil.sendPost(url, outBoundTaskToWcsVo);
        try {
            JSONObject jsonResponse = JSONUtil.parseObj(response);
            String code = jsonResponse.getStr("code");
            if ("200".equals(code)) {
                System.out.println("将信息成功发送至WCS系统，响应正常");
            } else {
                String message = jsonResponse.getStr("message", "未知错误");
                System.err.println("发送失败，错误码: " + code + ", 错误信息: " + message);
            }
        } catch (Exception e) {
            System.err.println("解析响应JSON失败: ");
        }
    }
    //outBoundCode 出库单编码 containerCode 载具编码 code 物料的二维码  weight填入重量
    @Override
    public OutBoundSotringDao outboundSort(String outBoundCode, String containerCode, String code, Double weight) throws IOException {
        if (outBoundCode==null){
            throw new RuntimeException("载具编码不能为空");
        }
        //已分拣
        Double sortedWeight= 0.0;
        OutBoundSotringDao outBoundSotringDao = new OutBoundSotringDao();
        ArrayList<OrDao> orDaos = new ArrayList<>();
        OutboundOrder outboundOrder = outboundOrderService.selectOne(new LambdaQueryWrapper<OutboundOrder>().eq(OutboundOrder::getOutboundOrderNo, outBoundCode));
        //查询载具
        ContainerDao containerDao = containerService.getOne(new LambdaQueryWrapper<ContainerDao>().eq(ContainerDao::getId, containerCode));
        List<MaterialDao> list = materialService.list(new LambdaQueryWrapper<MaterialDao>().eq(MaterialDao::getContainerCode, containerCode));
        //载具内的总重量
        double sum = list.stream().filter(e->e.getInventoryQuantity()!=null).filter(e -> e.getInventoryQuantity() > 0).mapToDouble(MaterialDao::getInventoryQuantity).sum();
        //查询分拣类型
        SortingDao sortingDao = sortingService.list(new LambdaQueryWrapper<SortingDao>().eq(SortingDao::getContainerId, containerCode)
                .eq(SortingDao::getOutboundCode, outBoundCode)).get(0);
        if (sortingDao!=null){
            outBoundSotringDao.setMaterialCode(sortingDao.getMaterialCode());
            outBoundSotringDao.setSortedWeight(0.00);
            outBoundSotringDao.setWaitSortWeight(sortingDao.getWeight());
            outBoundSotringDao.setSotringType(sortingDao.getSortType());
            outBoundSotringDao.setDesLocation(outboundOrder.getDestination());
            if (code!=null&&code!=""){
                OrDao orDao = scanOr(code, weight);
                orDaos.add(orDao);
                if (orDao!=null||orDao.getWeight()!=null){
                    Double sortingweight = orDao.getSortingweight();
                    outBoundSotringDao.setSortedWeight(sortingDao.getWeight());
                    outBoundSotringDao.setOrList(orDaos);
                    outBoundSotringDao.setWaitSortWeight(0.00);
                    outBoundSotringDao.setDesLocation(outboundOrder.getDestination());
                }
            }else {
                outBoundSotringDao.setOrList(orDaos);
            }
        }else{
            throw new RuntimeException("分拣载具不在此任务");
        }
        return outBoundSotringDao;
    }



    @Override
    public void lack(String materialCode, String taskId,Double weight) {
        Lack lack = new Lack();
        lack.setLackWeight(weight);
        lack.setTaskCode(taskId);
        lack.setLackWeight(weight);
        lack.setCreateName("Admin");
        lack.setCreateTime(new Date());
        lackService.save(lack);
    }

    private OrDao scanOr(String code,Double weight){
        OrDao orDao = new OrDao();
        MaterialDao materialByItemBarCode = materialService.findDataOne(code);
        if (materialByItemBarCode!=null){
            orDao.setOrCode(code);
            orDao.setMaterialCode(materialByItemBarCode.getMaterialCode());
            orDao.setMaterialName(materialByItemBarCode.getMaterialName());
            orDao.setWeight(materialByItemBarCode.getInventoryQuantity());
            orDao.setSortingweight(materialByItemBarCode.getInventoryQuantity());
        }
        return orDao;
    }
    /**
     * 验证任务合法性
     */
    private void validateTask(OutBoundTaskDao task) {
        // 检查紧急任务
        int status = 2;
        if (task.getPriority() == 1) {
            List<OutBoundTaskDao> executingTasks = baseMapper.selectList(
                    new LambdaQueryWrapper<OutBoundTaskDao>()
                            .eq(OutBoundTaskDao::getStatus, status) // 2 表示执行中
            );
            if (!executingTasks.isEmpty()) {
                throw new RuntimeException("当前有其他任务正在执行，紧急任务待插队执行");
            }
        }

//        // 检查库存   查询视图
//        List<ContainerDao> containers = containerService.list(
//                new LambdaQueryWrapper<ContainerDao>()
//                        .eq(ContainerDao::getId, material.getContainerCode())
//        );
//        if (containers.isEmpty()) {
//            throw new RuntimeException("物料库存不足，编码: " + task.getMaterialCode());
//        }
    }

    /**
     * 设置任务执行区域
     */
    private void setTaskArea(OutBoundTaskDao task) {
        if (task.getPriority() == 1) {
            // 紧急任务直接分配到分拣区
            task.setMaterHouse(ConstantStatus.SORTING_AREA);
        } else if (task.getPriority() == 2) {
            // 预约任务处理
            if ("M2".equals(task.getOutWarehouse())) {
                if (task.getRequireTime().before(new Date())) {
                    task.setMaterHouse(ConstantStatus.OPERATION_AREA);
                } else {
                    task.setMaterHouse(ConstantStatus.SORTING_AREA);
                }
            }
        }
    }

    /**
     * 分配储位
     */
    private void assignStorageLocation(OutBoundTaskDao task) throws InterruptedException {
        String storeType="";
        for (int retry = 0; retry < 3; retry++) {
            if (task.getOutboundPolicy()==2){
                storeType="4";
                List<StoreLocalDao> LocalDaolist = storeLocalService.list(
                        new LambdaQueryWrapper<StoreLocalDao>()
                                .eq(StoreLocalDao::getGoodType, storeType)
                                .eq(StoreLocalDao::getStatus, 0) // 0 表示可用
                );
                if (LocalDaolist.isEmpty()){
                    throw new  RuntimeException("当前没有可用储位");
                }
                StoreLocalDao localDao =  LocalDaolist.stream().findFirst().get();
                if (localDao != null) {
                    task.setOutsideId(localDao.getId());
                    // 更新储位状态为占用
                    localDao.setStatus(2); // 2 表示占用
                    localDao.setIsLock(1);
                    storeLocalService.updateById(localDao);
                    return;
                }
            }else {
                storeType = "1";
                List<StoreOutsideDao> list = storeOutsideService.list(
                        new LambdaQueryWrapper<StoreOutsideDao>()
                                .eq(StoreOutsideDao::getGoodType, storeType)
                                .eq(StoreOutsideDao::getStatus, 0) // 0 表示可用
                );
                if (list.isEmpty()) {
                    throw new RuntimeException("当前没有可用储位");
                }
                StoreOutsideDao outside = list.stream().findFirst().get();
                if (outside != null) {
                    task.setOutsideId(outside.getId());
                    // 更新储位状态为占用
                    outside.setStatus(2); // 2 表示占用
                    outside.setIsLock(1);
                    storeOutsideService.updateById(outside);
                    return;
                } else {
                    Thread.sleep(5000);//等待设定值
                    System.out.println("目前暂无空储位，请等待");
                }
            }
        }

    }

    /**
     * 选择分拣策略
     */
    @Transactional(rollbackFor = Exception.class)
    public void selectSortingStrategy(OutBoundTaskDao task) {
        MaterialDao material = materialMapper.selectList(
                new LambdaQueryWrapper<MaterialDao>()
                        .eq(MaterialDao::getMaterialCode, task.getMaterialCode())
        ).get(0);
        Double cumulative=0.00;
        List<ContainerDao> containers = containerService.list(
                new LambdaQueryWrapper<ContainerDao>().orderByDesc(ContainerDao::getCreateDate)
                        .eq(ContainerDao::getId, material.getContainerCode())
        );

        for (ContainerDao container : containers) {
            //判断此载具是否为混料
            List<String> materialCodeList=baseMapper.getcontainer(container.getId());

            if(task.getMaterilaNweight()-cumulative<=0){
                break;
            }
            if(container.getDisable()==1||container.getBarCodeBad()==1||container.getContainerBad()==1){
                throw new RuntimeException("该载具不可用！");
            }
            SortingDao sorting = new SortingDao();
            sorting.setMaterialCode(task.getMaterialCode());
            sorting.setContainerId(container.getId());
            sorting.setOutboundTaskCode(task.getCode());
            sorting.setCreateTime(new Date());
            sorting.setCreateName("SecurityUtils.getCurrentUsername()");
            sorting.setStatus(0);
            sorting.setWeight(container.getWeight());
            sorting.setOutboundCode(task.getProductOutBoundOrderNo());
            StoreLocalDao storeLocal = storageServer.getOne(
                    new LambdaQueryWrapper<StoreLocalDao>()
                            .eq(StoreLocalDao::getContainerCode, container.getId())
            );
            if (storeLocal==null){
                throw new RuntimeException("没有找到该储位！");
            }
            if (storeLocal != null){
                if ("M2".equals(storeLocal.getFrame())) {
                    sorting.setFloor("M2");
                    // 2F仓库分拣策略
                    if (task.getMaterilaNweight() < container.getWeight()) {
                        System.out.println("正向拣选");
                        sorting.setSortType(1);
                        cumulative+=container.getWeight();
                    } else {
                        sorting.setSortType(2);
                        System.out.println("整箱出库");
                        cumulative+=container.getWeight();
                    }
                } else {
                    sorting.setFloor("M1");
                    if ((task.getMaterilaNweight()-container.getWeight())<container.getWeight()/2){
                        System.out.println("反向拣选");
                        cumulative+=container.getWeight();
                        sorting.setSortType(0);
                    }else if((task.getMaterilaNweight()-container.getWeight())>container.getWeight()/2){
                        System.out.println("正向拣选");
                        cumulative+=container.getWeight();
                        sorting.setSortType(1);
                    }else {
                        System.out.println("整板出库");
                        cumulative+=container.getWeight();
                        sorting.setSortType(2);
                    }
                }
            }
            sortingService.save(sorting);


        }
    }
    //确认分拣类型并找出对应载具发送消息给AGV
    public void outboundHandling(OutBoundTaskDao outBoundTaskDao,String ip) throws IOException, InterruptedException {
        List<DispositionDao> dispositionDao1F = dispositionService.list(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getIp, ip)
                .eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"M1").eq(DispositionDao::getIsSorting,1));
        DispositionDao dispositionDao2F = dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getIp, ip)
                .eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"M2").eq(DispositionDao::getIsSorting,1));
        EndContainerDao endContainerDao = new EndContainerDao();
        endContainerDao.setCreateTime(new Date());
        List<SortingDao> sortingDaoList = sortingService.list(new LambdaQueryWrapper<SortingDao>().eq(SortingDao::getOutboundTaskCode, outBoundTaskDao.getCode()));
        Map<String, Map<Integer, Long>> floorTypeCountMap = sortingDaoList.stream()
                .collect(Collectors.groupingBy(
                        SortingDao::getFloor,
                        Collectors.groupingBy(SortingDao::getSortType, Collectors.counting())
                ));
        Map<Integer, Long> floor1Stats = floorTypeCountMap.getOrDefault("1F", Collections.emptyMap());
        long positiveCount = 0; // 正向数量
        long negativeCount = 0; // 反向数量
        long fullCount = 0; // 整箱数量
        // 使用for循环遍历Map
        for (Map.Entry<Integer, Long> entry : floor1Stats.entrySet()) {
            int type = entry.getKey();
            long count = entry.getValue();
            // 根据分拣类型统计数量
            switch (type) {
                case 0:
                    negativeCount += count;
                    break;
                case 1:
                    positiveCount += count;
                    break;
                case 2:
                    fullCount += count;
                    break;
            }
        }
        for (SortingDao sortingDao : sortingDaoList) {
            OutBoundTaskToWcsVo wcsVo = new OutBoundTaskToWcsVo();
            //判断是否为2F
            if (sortingDao.getFloor().equals("M2")) {
                //正向拣选
                wcsVo.setContainerCode(sortingDao.getContainerId());
                StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                wcsVo.setSource(storeLocalDao.getSerial());
                if (outBoundTaskDao.getOutboundPolicy()==1|| outBoundTaskDao.getOutboundPolicy()==3) {
                    wcsVo.setDestination(dispositionDao2F.getLocation());//终点
                    endContainerDao.setEndStore(dispositionDao2F.getLocation());
                }
                    endContainerDao.setContainerCode(sortingDao.getContainerId());
                    wcsVo.setTaskType("出库");
                    endContainerDao.setFloor("M2");
                    wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                    wcsVo.setFloor("M2");
                    wcsVo.setDeliveryType(2);
                    endContainerService.save(endContainerDao);
                    sendToWcs(wcsVo);
            }else {
                //1F
                //正反整都有
                if (positiveCount>0&&negativeCount>0&&fullCount>0){
                    // 整板
                    if (sortingDao.getSortType()==2){
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                        wcsVo.setSource(storeLocalDao.getSerial());
                        DispositionDao dispositionDao = dispositionDao1F.stream()
                                .filter(dao -> "整反分拣工位".equals(dao.getName()))
                                .findFirst()
                                .get();
                        wcsVo.setDestination(dispositionDao.getLocation());
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setTaskType("出库");
                        wcsVo.setFloor("M1");
                        wcsVo.setDeliveryType(2);
                        endContainerDao.setEndStore(dispositionDao.getLocation());
                        endContainerDao.setContainerCode(sortingDao.getContainerId());
                        endContainerDao.setFloor("M1");
                        //修改载具库外、库内
                        containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                        endContainerService.save(endContainerDao);containerService.getById(sortingDao.getContainerId());
                        sendToWcs(wcsVo);
                        //正向
                    }else if(sortingDao.getSortType()==1){
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                        wcsVo.setSource(storeLocalDao.getSerial());
                        Optional<DispositionDao> positiveDaoOpt = dispositionDao1F.stream()
                                .filter(dao -> "正分拣工位".equals(dao.getName()))
                                .findFirst();
                        // 获取反分拣工位
                        Optional<DispositionDao> reverseDaoOpt = dispositionDao1F.stream()
                                .filter(dao -> "反分拣回库工位".equals(dao.getName()))
                                .findFirst();
                        if (positiveDaoOpt.isPresent()) {
                            DispositionDao positiveDao = positiveDaoOpt.get();
                            endContainerDao.setEndStore(positiveDao.getLocation());
                            wcsVo.setDestination(positiveDao.getLocation());
                        } else {
                            DispositionDao reverseDao = reverseDaoOpt.get();
                            endContainerDao.setEndStore(reverseDao.getLocation());
                            wcsVo.setDestination(reverseDao.getLocation());
                        }
                        wcsVo.setDestination( positiveDaoOpt.get().getLocation());
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setTaskType("出库");
                        wcsVo.setFloor("M1");
                        wcsVo.setDeliveryType(2);
                        endContainerDao.setEndStore( positiveDaoOpt.get().getLocation());
                        endContainerDao.setContainerCode(sortingDao.getContainerId());
                        containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                        endContainerDao.setFloor("M1");
                        endContainerService.save(endContainerDao);
                        sendToWcs(wcsVo);
                        //反向
                    }else {
                        //先查询反向回库工位有没有料
                        Optional<DispositionDao> reverseDaoOpt = dispositionDao1F.stream()
                                .filter(dao -> "反分拣回库工位".equals(dao.getName()))
                                .findFirst();
                        if (reverseDaoOpt.isPresent()) {
                            Thread.sleep(5000);
                        }
                        DispositionDao dispositionDao = dispositionDao1F.stream()
                                .filter(dao -> "整反分拣工位".equals(dao.getName()))
                                .findFirst()
                                .get();
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                        wcsVo.setSource(storeLocalDao.getSerial());
                        wcsVo.setDestination(dispositionDao.getLocation());
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setTaskType("出库");
                        wcsVo.setFloor("M1");
                        wcsVo.setDeliveryType(2);
                        endContainerDao.setEndStore(dispositionDao.getLocation());
                        endContainerDao.setContainerCode(sortingDao.getContainerId());
                        endContainerDao.setFloor("M1");
                        //修改载具库外、库内
                        containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                        endContainerService.save(endContainerDao);containerService.getById(sortingDao.getContainerId());
                        sendToWcs(wcsVo);
                    }

                }
                //正反都有
                else if (positiveCount>0&&negativeCount<0&&fullCount<0){
                    //正向
                    if(sortingDao.getSortType()==1){
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                        wcsVo.setSource(storeLocalDao.getSerial());
                        DispositionDao dispositionDao = dispositionDao1F.stream()
                                .filter(dao -> "正分拣工位".equals(dao.getName()))
                                .findFirst()
                                .get();
                        wcsVo.setDestination(dispositionDao.getLocation());
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setTaskType("出库");
                        wcsVo.setFloor("M1");
                        wcsVo.setDeliveryType(2);
                        endContainerDao.setEndStore(dispositionDao.getLocation());
                        endContainerDao.setContainerCode(sortingDao.getContainerId());
                        endContainerDao.setFloor("M1");
                        //修改载具库外、库内
                        containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                        endContainerService.save(endContainerDao);containerService.getById(sortingDao.getContainerId());
                        sendToWcs(wcsVo);
                        //反向
                    } else if (sortingDao.getSortType()==0) {
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                        wcsVo.setSource(storeLocalDao.getSerial());
                        DispositionDao dispositionDao = dispositionDao1F.stream()
                                .filter(dao -> "整反分拣工位".equals(dao.getName()))
                                .findFirst()
                                .get();
                        wcsVo.setDestination(dispositionDao.getLocation());
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setTaskType("出库");
                        wcsVo.setFloor("M1");
                        wcsVo.setDeliveryType(2);
                        endContainerDao.setEndStore(dispositionDao.getLocation());
                        endContainerDao.setContainerCode(sortingDao.getContainerId());
                        endContainerDao.setFloor("M1");
                        //修改载具库外、库内
                        containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                        endContainerService.save(endContainerDao);containerService.getById(sortingDao.getContainerId());
                        sendToWcs(wcsVo);
                    }
                    //整箱与正
                } else if (positiveCount>0&&fullCount>0&&negativeCount<0){
                    // 整板
                    if (sortingDao.getSortType()==2){
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                        wcsVo.setSource(storeLocalDao.getSerial());
                        DispositionDao dispositionDao = dispositionDao1F.stream()
                                .filter(dao -> "整反分拣工位".equals(dao.getName()))
                                .findFirst()
                                .get();
                        wcsVo.setDestination(dispositionDao.getLocation());
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setTaskType("出库");
                        wcsVo.setFloor("M1");
                        wcsVo.setDeliveryType(2);
                        endContainerDao.setEndStore(dispositionDao.getLocation());
                        endContainerDao.setContainerCode(sortingDao.getContainerId());
                        endContainerDao.setFloor("M1");
                        //修改载具库外、库内
                        containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                        endContainerService.save(endContainerDao);containerService.getById(sortingDao.getContainerId());
                        sendToWcs(wcsVo);
                        //正向
                    }else if(sortingDao.getSortType()==1){
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                        wcsVo.setSource(storeLocalDao.getSerial());
                        Optional<DispositionDao> positiveDaoOpt = dispositionDao1F.stream()
                                .filter(dao -> "正分拣工位".equals(dao.getName()))
                                .findFirst();
                        // 获取反分拣工位
                        Optional<DispositionDao> reverseDaoOpt = dispositionDao1F.stream()
                                .filter(dao -> "反分拣回库工位".equals(dao.getName()))
                                .findFirst();
                        if (positiveDaoOpt.isPresent()) {
                            DispositionDao positiveDao = positiveDaoOpt.get();
                            endContainerDao.setEndStore(positiveDao.getLocation());
                            wcsVo.setDestination(positiveDao.getLocation());
                        } else {
                            DispositionDao reverseDao = reverseDaoOpt.get();
                            endContainerDao.setEndStore(reverseDao.getLocation());
                            wcsVo.setDestination(reverseDao.getLocation());
                        }
                        wcsVo.setDestination( positiveDaoOpt.get().getLocation());
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setTaskType("出库");
                        wcsVo.setFloor("M1");
                        wcsVo.setDeliveryType(2);
                        endContainerDao.setEndStore( positiveDaoOpt.get().getLocation());
                        endContainerDao.setContainerCode(sortingDao.getContainerId());
                        containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                        endContainerDao.setFloor("M1");
                        endContainerService.save(endContainerDao);
                        sendToWcs(wcsVo);
                        //反向
                    }
                }else if (negativeCount>0&&fullCount<0&&positiveCount<0){
                    wcsVo.setContainerCode(sortingDao.getContainerId());
                    StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                    wcsVo.setSource(storeLocalDao.getSerial());
                    DispositionDao dispositionDao = dispositionDao1F.stream()
                            .filter(dao -> "整反分拣工位".equals(dao.getName()))
                            .findFirst()
                            .get();
                    wcsVo.setDestination(dispositionDao.getLocation());
                    wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                    wcsVo.setTaskType("出库");
                    wcsVo.setFloor("M1");
                    wcsVo.setDeliveryType(2);
                    endContainerDao.setEndStore(dispositionDao.getLocation());
                    endContainerDao.setContainerCode(sortingDao.getContainerId());
                    endContainerDao.setFloor("M1");
                    //修改载具库外、库内
                    containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                    endContainerService.save(endContainerDao);containerService.getById(sortingDao.getContainerId());
                    sendToWcs(wcsVo);
                } else if (negativeCount<0&&positiveCount<0&&fullCount>0){
                    wcsVo.setContainerCode(sortingDao.getContainerId());
                    StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                    wcsVo.setSource(storeLocalDao.getSerial());
                    DispositionDao dispositionDao = dispositionDao1F.stream()
                            .filter(dao -> "整反分拣工位".equals(dao.getName()))
                            .findFirst()
                            .get();
                    wcsVo.setDestination(dispositionDao.getLocation());
                    wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                    wcsVo.setTaskType("出库");
                    wcsVo.setFloor("M1");
                    wcsVo.setDeliveryType(2);
                    endContainerDao.setEndStore(dispositionDao.getLocation());
                    endContainerDao.setContainerCode(sortingDao.getContainerId());
                    endContainerDao.setFloor("M1");
                    //修改载具库外、库内
                    containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                    endContainerService.save(endContainerDao);containerService.getById(sortingDao.getContainerId());
                    sendToWcs(wcsVo);
                } else if (negativeCount<0&&positiveCount>0&&fullCount<0){
                    //全正
                    wcsVo.setContainerCode(sortingDao.getContainerId());
                    StoreLocalDao storeLocalDao = storeLocalService.getOne(new LambdaQueryWrapper<StoreLocalDao>().eq(StoreLocalDao::getContainerCode, sortingDao.getContainerId()));
                    wcsVo.setSource(storeLocalDao.getSerial());
                    Optional<DispositionDao> positiveDaoOpt = dispositionDao1F.stream()
                            .filter(dao -> "正分拣工位".equals(dao.getName()))
                            .findFirst();
                    // 获取反分拣工位
                    Optional<DispositionDao> reverseDaoOpt = dispositionDao1F.stream()
                            .filter(dao -> "反分拣回库工位".equals(dao.getName()))
                            .findFirst();

                    Optional<DispositionDao> wholeplateeDaoOpt = dispositionDao1F.stream()
                            .filter(dao -> "整反分拣工位".equals(dao.getName()))
                            .findFirst();
                    if (positiveDaoOpt.isPresent()) {
                        DispositionDao positiveDao = positiveDaoOpt.get();
                        endContainerDao.setEndStore(positiveDao.getLocation());
                        wcsVo.setDestination(positiveDao.getLocation());
                    } else if (reverseDaoOpt.isPresent()) {
                        DispositionDao positiveDao = reverseDaoOpt.get();
                        endContainerDao.setEndStore(positiveDao.getLocation());
                        wcsVo.setDestination(positiveDao.getLocation());
                    } else {
                        DispositionDao reverseDao = wholeplateeDaoOpt.get();
                        endContainerDao.setEndStore(reverseDao.getLocation());
                        wcsVo.setDestination(reverseDao.getLocation());
                    }
                    wcsVo.setDestination( positiveDaoOpt.get().getLocation());
                    wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                    wcsVo.setTaskType("出库");
                    wcsVo.setFloor("M1");
                    wcsVo.setDeliveryType(2);
                    endContainerDao.setEndStore( positiveDaoOpt.get().getLocation());
                    endContainerDao.setContainerCode(sortingDao.getContainerId());
                    containerService.updateById(containerService.getById(sortingDao.getContainerId()).setLocal("库外"));
                    endContainerDao.setFloor("M1");
                    endContainerService.save(endContainerDao);
                    sendToWcs(wcsVo);
                }
            }

        }
    }
    private static Double materilaNweight = 0.00 ;

    //分拣  endStore为分拣任务结束终点也就是工位的储位码  realWeight为人员实际称重  storeCode为人员分拣完后扫的栈板码 splitWeight需要分拣出的重量
    public void sorting(String endStore,Double realWeight,String storeCode,Float splitWeight,String ip) throws IOException {
        String username = null;
        try {
            username = SecurityUtils.getCurrentUser().getUsername();
        } catch (Exception e) {
            username = "system";
        }
        EndContainerDao endContainerDao = endContainerService.getOne(new LambdaQueryWrapper<EndContainerDao>().eq(EndContainerDao::getEndStore, endStore));
        SortingDao sortingDao = sortingService.getOne(new LambdaQueryWrapper<SortingDao>().eq(SortingDao::getContainerId, endContainerDao.getContainerCode()));
        OutBoundTaskDao outBoundTaskDao = outBoundTaskMapper.selectOne(new LambdaQueryWrapper<OutBoundTaskDao>().eq(OutBoundTaskDao::getCode, sortingDao.getOutboundTaskCode()));
        OutBoundTaskDao outBoundTaskDaoByName = outBoundTaskMapper.selectOne(new LambdaQueryWrapper<OutBoundTaskDao>().eq(OutBoundTaskDao::getStartName, username));
        OutBoundTaskToWcsVo wcsVo = new OutBoundTaskToWcsVo();
        if (outBoundTaskDaoByName ==null){
            throw new RuntimeException("当前登录用户不是开启分拣任务用户，请更换账号重试");
        }

            if (sortingDao.getFloor().equals("2F")){
                //判断是否为整箱
                if (sortingDao.getSortType()==2){
                    if (realWeight!=null){
                        //数量有差异
                        System.out.println("输入实际重量调用黑湖");
                        //通过工位ip找到起点
                        wcsVo.setSource(dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"2F").eq(DispositionDao::getIsSorting,0)).getLocation());
                        wcsVo.setDeliveryType(2);
                        wcsVo.setFloor("2F");
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        wcsVo.setTaskType("出库");
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setSource(endStore);
                        //线边
                        wcsVo.setDestination("");
                        sendToWcs(wcsVo);
                        sortingDao.setStatus(1);
                        sortingService.updateById(sortingDao);
                        outBoundTaskDao.setSorted(realWeight);
                        outBoundTaskDao.setUpdateName(SecurityUtils.getCurrentUser().getUsername());
                        outBoundTaskDao.setUpdateTime(new Date());
                        baseMapper.updateById(outBoundTaskDao);
                    }else {
                        //有码
                        if (storeCode!=null){
                            ContainerDao containerDao = containerService.getById(sortingDao.getContainerId());
                            materilaNweight+=containerDao.getWeight();
                            wcsVo.setSource(dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"2F").eq(DispositionDao::getIsSorting,0)).getLocation());
                            wcsVo.setDeliveryType(2);
                            wcsVo.setFloor("2F");
                            wcsVo.setContainerCode(sortingDao.getContainerId());
                            wcsVo.setTaskType("出库");
                            wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                            wcsVo.setSource(endStore);
                            //线边
                            wcsVo.setDestination("");
                            sendToWcs(wcsVo);
                            sortingDao.setStatus(1);
                            sortingService.updateById(sortingDao);
                            outBoundTaskDao.setSorted(realWeight);
                            outBoundTaskDao.setUpdateName(SecurityUtils.getCurrentUser().getUsername());
                            outBoundTaskDao.setUpdateTime(new Date());
                            baseMapper.updateById(outBoundTaskDao);
                        }
                    }
                }else {//正向
                    if (realWeight!=null){
                        //数量有差异
                        System.out.println("输入实际重量调用黑湖");
                        wcsVo.setDeliveryType(2);
                        wcsVo.setSource(dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"2F").eq(DispositionDao::getIsSorting,0)).getLocation());
                        wcsVo.setFloor("2F");
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        wcsVo.setTaskType("出库");
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setSource(endStore);
                        //线边
                        wcsVo.setDestination("");
                        sendToWcs(wcsVo);
                        sortingDao.setStatus(1);
                        sortingService.updateById(sortingDao);
                        outBoundTaskDao.setSorted(realWeight);
                        outBoundTaskDao.setUpdateName(SecurityUtils.getCurrentUser().getUsername());
                        outBoundTaskDao.setUpdateTime(new Date());
                        baseMapper.updateById(outBoundTaskDao);
                    }else {
                        if (splitWeight!=null){
                            split(splitWeight,sortingDao.getContainerId());
                        }
                        ContainerDao containerDao = containerService.getById(sortingDao.getContainerId());
                        materilaNweight+=containerDao.getWeight();
                        wcsVo.setSource(dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"2F").eq(DispositionDao::getIsSorting,0)).getLocation());
                        wcsVo.setDeliveryType(2);
                        wcsVo.setFloor("2F");
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        wcsVo.setTaskType("出库");
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setSource(endStore);
                        //线边
                        wcsVo.setDestination("");
                        sendToWcs(wcsVo);
                        sortingDao.setStatus(1);
                        sortingService.updateById(sortingDao);
                        outBoundTaskDao.setSorted(realWeight);
                        outBoundTaskDao.setUpdateName(SecurityUtils.getCurrentUser().getUsername());
                        outBoundTaskDao.setUpdateTime(new Date());
                        baseMapper.updateById(outBoundTaskDao);
                    }
                }
            }else {
                //1F
                //整板
                if (sortingDao.getSortType()==2){
                    if (realWeight!=null){
                        //数量有差异
                        System.out.println("输入实际重量调用黑湖");
                    }else {
                        //有码
                        if (storeCode!=null){
                            ContainerDao containerDao = containerService.getById(sortingDao.getContainerId());
                            materilaNweight+=containerDao.getWeight();
                            wcsVo.setDeliveryType(2);
                            wcsVo.setSource(dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"1F").eq(DispositionDao::getIsSorting,0)).getLocation());
                            wcsVo.setFloor("1F");
                            wcsVo.setContainerCode(sortingDao.getContainerId());
                            wcsVo.setTaskType("出库");
                            wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                            wcsVo.setSource(endStore);
                            //线边
                            wcsVo.setDestination("");
                            sendToWcs(wcsVo);
                            sortingDao.setStatus(1);
                            sortingService.updateById(sortingDao);
                            outBoundTaskDao.setSorted(realWeight);
                            outBoundTaskDao.setUpdateName(SecurityUtils.getCurrentUser().getUsername());
                            outBoundTaskDao.setUpdateTime(new Date());
                            baseMapper.updateById(outBoundTaskDao);
                        }
                    }
                }else if(sortingDao.getSortType()==1){
                    //正向
                    if (realWeight!=null){
                        //数量有差异
                        System.out.println("输入实际重量调用黑湖");
                        wcsVo.setSource(dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"1F").eq(DispositionDao::getIsSorting,0)).getLocation());
                        wcsVo.setDeliveryType(2);
                        wcsVo.setFloor("1F");
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        wcsVo.setTaskType("出库");
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setSource(endStore);
                        //线边
                        wcsVo.setDestination("");
                        sendToWcs(wcsVo);
                        sortingDao.setStatus(1);
                        sortingService.updateById(sortingDao);
                        outBoundTaskDao.setSorted(realWeight);
                        outBoundTaskDao.setUpdateName(SecurityUtils.getCurrentUser().getUsername());
                        outBoundTaskDao.setUpdateTime(new Date());
                        baseMapper.updateById(outBoundTaskDao);
                    }else {
                        //有码
                        if (storeCode!=null){
                            ContainerDao containerDao = containerService.getById(sortingDao.getContainerId());
                            materilaNweight+=containerDao.getWeight();
                            wcsVo.setDeliveryType(2);
                            wcsVo.setSource(dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"1F").eq(DispositionDao::getIsSorting,0)).getLocation());
                            wcsVo.setFloor("1F");
                            wcsVo.setContainerCode(sortingDao.getContainerId());
                            wcsVo.setTaskType("出库");
                            wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                            wcsVo.setSource(endStore);
                            //线边
                            wcsVo.setDestination("");
                            sendToWcs(wcsVo);
                            sortingDao.setStatus(1);
                            sortingService.updateById(sortingDao);
                            outBoundTaskDao.setSorted(realWeight);
                            outBoundTaskDao.setUpdateName(SecurityUtils.getCurrentUser().getUsername());
                            outBoundTaskDao.setUpdateTime(new Date());
                            baseMapper.updateById(outBoundTaskDao);
                        }
                    }
                }else {
                    //反向
                    if (realWeight!=null){
                        //数量有差异
                        System.out.println("输入实际重量调用黑湖");
                        wcsVo.setSource(dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"1F").eq(DispositionDao::getIsSorting,0)).getLocation());
                        wcsVo.setDeliveryType(2);
                        wcsVo.setFloor("1F");
                        wcsVo.setContainerCode(sortingDao.getContainerId());
                        wcsVo.setTaskType("出库");
                        wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                        wcsVo.setSource(endStore);
                        //线边
                        wcsVo.setDestination("");
                        sendToWcs(wcsVo);
                        sortingDao.setStatus(1);
                        sortingService.updateById(sortingDao);
                        outBoundTaskDao.setSorted(realWeight);
                        outBoundTaskDao.setUpdateName(SecurityUtils.getCurrentUser().getUsername());
                        outBoundTaskDao.setUpdateTime(new Date());
                        baseMapper.updateById(outBoundTaskDao);
                    }else {
                        //有码
                        if (storeCode!=null){
                            ContainerDao containerDao = containerService.getById(sortingDao.getContainerId());
                            materilaNweight+=containerDao.getWeight();
                            wcsVo.setSource(dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>().eq(DispositionDao::getStatus,0).eq(DispositionDao::getFloor,"1F").eq(DispositionDao::getIsSorting,0)).getLocation());
                            wcsVo.setDeliveryType(2);
                            wcsVo.setFloor("1F");
                            wcsVo.setContainerCode(sortingDao.getContainerId());
                            wcsVo.setTaskType("出库");
                            wcsVo.setPriority(outBoundTaskDao.getPriority());//优先级
                            wcsVo.setSource(endStore);
                            //线边
                            wcsVo.setDestination("");
                            sendToWcs(wcsVo);
                            sortingDao.setStatus(1);
                            sortingService.updateById(sortingDao);
                            outBoundTaskDao.setSorted(realWeight);
                            outBoundTaskDao.setUpdateName(SecurityUtils.getCurrentUser().getUsername());
                            outBoundTaskDao.setUpdateTime(new Date());
                            baseMapper.updateById(outBoundTaskDao);
                        }
                    }
                }
            }
            if(materilaNweight-outBoundTaskDao.getMaterilaNweight()>1){
                System.out.println("分拣重量超过任务重量，请重新分拣此单");
            }else {
                System.out.println("分拣完成");
                outBoundTaskDao.setStatus(1);
                baseMapper.updateById(outBoundTaskDao);
            }


    }
public void lack(String taskCode,Double lackWeight){
    // 报缺
    Lack lack = new Lack();
    lack.setLackWeight(lackWeight);
    lack.setCreateTime(new Date());
    lack.setCreateName(SecurityUtils.getCurrentUser().getUsername());
    lack.setTaskCode(taskCode);
    lackService.save(lack);
}
public void split(Float splitWeight,String sortingCode){
    System.out.println("生成物料条码");

}

}




